import 'package:f_enjoy_plus_community/utils/toast.dart';
import 'package:flutter/material.dart';

import '../../utils/http.dart';
import 'components/house_item.dart';

class HouseList extends StatefulWidget {
  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {

  List houseList=[];
  //获取房屋列表
  void getHouseList() async{
    try {
      var res =await http.get('/room');
      if(res['code']!=10000) return ToastUtil.showError('获取房屋列表失败');
      setState(() {
        houseList = res['data'];
      });
      ToastUtil.showSuccess('获取房屋列表成功');
    } catch (e) {
      ToastUtil.showError('网络请求出现问题');
    }

  }

  @override
  void initState() {
    super.initState();
    getHouseList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '我的房屋',
          style: TextStyle(color: Colors.black, fontSize: 18),
        ),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),

      //添加过度效果
      body: houseList.isEmpty?Center(
        child: CircularProgressIndicator(
          valueColor: AlwaysStoppedAnimation(Colors.black),
        ),
      ):Container(
        padding: EdgeInsets.all(10),
        child: Stack(
          alignment: AlignmentDirectional.bottomEnd,
          children: [
            ListView.separated(
              //返回列表元素
              itemBuilder: (context, index) {
                return HouseItem(houseInfo: houseList[index],);
              },
              //返回分割线
              separatorBuilder: (context, index){
                return Container(height: 10, color: Colors.grey);
              },
              itemCount: houseList.length,
            ),
            Positioned(
              child: Container(
                padding: EdgeInsets.all(10),
                height: 100,
                width: MediaQuery.of(context).size.width,
                child: ElevatedButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/location_list');
                  },
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [Icon(Icons.add), Text('添加房屋')],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
